#include "stdafx.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif
/////////////////////////////////////////////////////////////////////////////////////////////////
void calc_emv(tagUMyCalcFormula*pFormula,long numCalc,phistory_data paData,double*dataCalc[10],long decNum,long sec_type,_procReflection procReflection)
{
	long i=0;
	double seedPrice(__price_seed__[decNum]);
	double*MID=dataCalc[5],high_low=0,high_low_last=0;
	double*VOLUME=dataCalc[2],*EMV=dataCalc[0];
	double*pcdVolume=dataCalc[6];
	for(;i<numCalc;++i)
	{
		high_low = (paData[i].highPx+paData[i].lowPx)/seedPrice;
		if(i)
		{
			if(high_low>0.0001f) MID[i] = 100*(high_low-high_low_last)/high_low;
			else MID[i] = MID[i-1];
		}
		high_low_last = high_low;
		dataCalc[4][i] = (paData[i].highPx-paData[i].lowPx)/seedPrice;
		pcdVolume[i] = (double)paData[i].totalVolumeTrade;
	}
	memcpy(dataCalc[3],pcdVolume,numCalc*sizeof(double));
	UMyCalc_MA(dataCalc[3],numCalc,pFormula->infoParam[0].nowValue);
	UMyCalc_MA(dataCalc[4],numCalc,pFormula->infoParam[0].nowValue);
	UMyCalc_DIV(VOLUME,dataCalc[3],pcdVolume,numCalc);

	for(i=0;i<numCalc&&(UMyCalc_invalid(dataCalc[3][i])||
		UMyCalc_invalid(dataCalc[4][i])||
		UMyCalc_invalid(dataCalc[5][i]));++i);
	for(;i<numCalc;++i)
	{
		if(dataCalc[4][i]>0.0001f)
			EMV[i] = MID[i]*VOLUME[i]*
				(paData[i].highPx-paData[i].lowPx)/seedPrice/dataCalc[4][i];
		else if(i) EMV[i] = EMV[i-1];
	}
	UMyCalc_MA(EMV,numCalc,pFormula->infoParam[0].nowValue);

	memcpy(dataCalc[1],EMV,numCalc*sizeof(double));
	UMyCalc_MA(dataCalc[1],numCalc,pFormula->infoParam[1].nowValue);
}
/*
VOLUME:=MA(VOL,N)/VOL;
MID:=100*(HIGH+LOW-REF(HIGH+LOW,1))/(HIGH+LOW);
EMV:MA(MID*VOLUME*(HIGH-LOW)/MA(HIGH-LOW,N),N);
MAEMV:MA(EMV,M);
*/
